//package com.mccken.demo.io.netty.server;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import com.mccken.demo.io.netty.support.RpcRequest;
//import com.mccken.demo.io.netty.support.RpcResponse;
//import com.mccken.demo.io.netty.util.ClassUtil;
//
//import io.netty.channel.ChannelHandlerContext;
//import io.netty.channel.SimpleChannelInboundHandler;
//import lombok.extern.slf4j.Slf4j;
//
///**
// * @program: mccken-collect
// * @description:
// * @author: mccken
// * @create: 2022-04-30 18:44
// **/
//@Slf4j
//public class ServerHandler extends SimpleChannelInboundHandler<RpcRequest> {
//	
//	private static final Map<Class<?>, Object> OBJECT_MAP = new HashMap<>(); 
//	
//	
//	@Override
//	protected void channelRead0(ChannelHandlerContext ctx, RpcRequest rpcRequest) throws Exception {
//		RpcResponse rpcResponse = new RpcResponse();
//		rpcResponse.setRequestId(rpcRequest.getRequestId());
//		log.info("开始处理消息: requestId = " + rpcRequest.getRequestId());
//
//		try {
//
//			Class<?> clazz = Class.forName(rpcRequest.getClassName());
//			if (!OBJECT_MAP.containsKey(clazz)) {
//			// 获取接口的实现类，这里只获取第一个实现类，忽略其他实现类
//				List<Class> allClassByInterface = ClassUtil.getAllClassByInterface(clazz);
//				for (Class c : allClassByInterface) {
//					OBJECT_MAP.put(clazz, c.newInstance());
//					break;	
//				}
//			}
//		    // 通过反射找到方法执行
//			Method method = clazz.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
//			method.setAccessible(true);
//			Object result = method.invoke(OBJECT_MAP.get(clazz), rpcRequest.getParameters());
//			rpcResponse.setResult(result);	
//		} catch (Exception e) {
//			log.error("处理失败... requestId = " + rpcRequest.getRequestId(), e);
//			rpcResponse.setErrorMsg("error");
//		}
//		
//		ctx.writeAndFlush(rpcResponse);
//
//	}
//}
